package cn.lijigou.shopping;

import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.domain.User;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.service.ServerService;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import cn.lijigou.shopping.util.StringTool;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author 董永健 E-mail:dong_yong_jian@163.com
 * @version CreatTime：2011-3-30 上午11:27:24 注册
 */
public class RegisterActivity extends MActivity implements OnClickListener {
	private TextView back= null;
	private LinearLayout titleLeftView= null;
	private Button register= null;
	private TextView title_center= null;
	private TextView title_right= null;
	private TextView error_content= null;
	
	private String result = "";
	private EditText register_account = null;
	private EditText register_password1 = null;
	private EditText register_password2 = null;
	private LinearLayout bottom_linear = null;
	private LinearLayout error_linear = null;
	private User user = null;

	private String sessionKey;
	private String nickname;
	private String id;
	private String mAccount;
	private String mPasswd1;
	private String mPasswd2;
	private String session;
	private String uid;
	private ProgressDialog mProgress = null;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();

	public Handler resultHander = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				resultAlert(result);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		this.setContentView(R.layout.register);
		overridePendingTransition(R.anim.push_in,R.anim.push_out);
		setView();
		setOnClickListeners();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	private void setView() {
		back = (TextView) this.findViewById(R.id.title_btn_left);
		back.setBackgroundResource(R.drawable.title_back_normal);
		titleLeftView = (LinearLayout) this.findViewById(R.id.titleLeftView);
		
		title_right = (TextView) this.findViewById(R.id.title_btn_right);
		title_right.setVisibility(View.GONE);
		title_center = (TextView) this.findViewById(R.id.text_center);
		title_center.setText(R.string.register);
		findViewById(R.id.titleRightView).setVisibility(View.GONE);
		
		register_account = (EditText) findViewById(R.id.register_account);
		register_password1 = (EditText) findViewById(R.id.register_password1);
		register_password2 = (EditText) findViewById(R.id.register_password2);

		bottom_linear = (LinearLayout) findViewById(R.id.bottom_linear);
		error_linear = (LinearLayout) findViewById(R.id.error_linear);
		register = (Button) findViewById(R.id.register_btn);
		error_content = (TextView) findViewById(R.id.error_content);

	}
	private void  setOnClickListeners(){
		titleLeftView.setOnClickListener(this);
		register.setOnClickListener(this);
	}

 	/**
	 * 显示等待框
	 */
	protected void showProgress() {
		this.runOnUiThread(new Runnable() {
			public void run() {	
				mProgress = new ProgressDialog(RegisterActivity.this);
				mProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);
				mProgress.setMessage("正在注册，请稍后...");
				mProgress.show();
			}
		});
	}

	/**
	 * 取消等待框
	 */
	protected void hideProgress() {
		
		this.runOnUiThread(new Runnable() {
			public void run() {
				if (mProgress != null) {
					mProgress.dismiss();
				}
			}
		});
	}
	
	/**
	 * @author 董永健 结果提示
	 * @param result
	 * @param
	 */
	public void resultAlert(String result) {
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast_error,
				(ViewGroup) findViewById(R.id.llToast));
		ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
		image.setImageResource(R.drawable.icon);
		TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
		TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
		if ("".equals(result)) {
			title.setText(R.string.registration_is_successful);
			text.setText(R.string.thanks_for_using);
			SharedPreferences sp = RegisterActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
			Editor edit = sp.edit();
			edit.clear().commit();
			edit.putString("uid",user.getId());
			edit.putString("session",user.getSessionKey());
			edit.putString("nickname",user.getNickname());
			edit.putString("isTmp",user.getIsTmp());
			edit.commit();
			Toast toast = new Toast(this);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();
			this.finish();
		} else if ("#1006".equals(result)) {
			error_linear.setVisibility(View.VISIBLE);
			error_content.setText(R.string.existing_email);
		} else if ("#1008".equals(result)) {
			error_linear.setVisibility(View.VISIBLE);
			error_content.setText(R.string.existing_mobile_phone);
		} else if ("#1007".equals(result)) {
			error_linear.setVisibility(View.VISIBLE);
			error_content.setText(R.string.user_name_is_invalid);
		}
		
	}

	public void showError(int id) {
		switch (id) {
		case 1:
			error_content.setText(R.string.account_can_not_be_empty);
			break;
		case 2:
			error_content.setText(R.string.e_mail_account_does_not_meet_the_format);
			break;
		case 3:
			error_content.setText(R.string.nickname_can_not_be_empty);
			break;
		case 4:
			error_content.setText(R.string.nickname_requirements);
			break;
		case 5:
			error_content.setText(R.string.password_can_not_be_empty);
			break;
		case 6:
			error_content.setText(R.string.repeat_the_password);
			break;
		case 7:
			error_content.setText(R.string.enter_the_password_twice_in_different);
			break;
		case 8:
			error_content.setText(R.string.e_mail_or_phone_number_is_wrong);
			break;
		case 9:
			error_content.setText(R.string.address_can_not_be_empty);
			break;
		case 10:
			error_content.setText(R.string.password_does_not_meet_the_rules);
			break;
		}
		error_linear.setVisibility(View.VISIBLE);
	}

	public void onClick(View v) {
		MSystem.pv += 1;
		switch (v.getId()) {
		case R.id.titleLeftView :
			this.finish();
			break;
		case R.id.register_btn :
			mAccount = register_account.getText().toString();
			mPasswd1 = register_password1.getText().toString();
			mPasswd2 = register_password2.getText().toString();
			if (StringTool.isNull(mAccount)) {
				showError(1);
			} else if(!StringTool.isEmail(mAccount) && !StringTool.isMobileNO(mAccount)){
				showError(8);
			} else if (StringTool.isNull(mPasswd1)) {
				showError(5);
			} else if (StringTool.isLength6_16(mPasswd1)) {
				showError(10);
			} else if (StringTool.isNull(mPasswd2)) {
				showError(6);
			} else if (StringTool.isSamePassword(mPasswd1, mPasswd2)) {
				showError(7);
			} else {
				user = new User(sessionKey, nickname, id);
				executorService.execute(new Thread() {
					public void run() {
						showProgress();

						MResult mResult = MSystem.Register(user, mAccount, mPasswd1);
						result = mResult.getErrorCode();
						resultHander.sendEmptyMessage(1);

						hideProgress();

					}
				});
			}

			break;
		}

	}
}
